package com.LearnInWorld.video.service.impl;

import com.LearnInWorld.video.mapper.CategoryMapper;
import com.LearnInWorld.video.pojo.Category;
import com.LearnInWorld.video.service.CategoryService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
import java.util.Map;

@Service
public class CategoryServiceImpl implements CategoryService {
    @Autowired
    private CategoryMapper categoryMapper;

    /***
     * 查询所有
     * @return
     */
    @Override
    public List<Category> findAll() {
        List<Category> categoryList = categoryMapper.selectAll();
        return categoryList;
    }

    /**
     * 根据ID查询
     *
     * @param id
     * @return
     */
    @Override
    public Category findById(Long id) {
        Category category = categoryMapper.selectByPrimaryKey(id);
        return category;
    }

    /***
     * 新增
     * @param category
     */
    @Override
    public void add(Category category) {
        categoryMapper.insertSelective(category);
    }

    /***
     * 修改
     * @param category
     */
    @Override
    public void update(Category category, Long Id) {
        category.setId(Id);
        categoryMapper.updateByPrimaryKey(category);
    }

    /***
     * 删除
     * @param id
     */
    @Override
    public void delete(Long id) {
        categoryMapper.deleteByPrimaryKey(id);
    }

    /***
     * 多条件搜索
     * @param searchMap
     * @return
     */
    @Override
    public List<Category> findList(Map<String, Object> searchMap) {
        Example example = createExample(searchMap);
        return categoryMapper.selectByExample(example);
    }


    /***
     * 分页查询
     * @param page
     * @param size
     * @return
     */
    @Override
    public Page<Category> findPage(int page, int size) {
        PageHelper.startPage(page, size);
        return (Page<Category>) categoryMapper.selectAll();
    }

    /***
     * 多条件分页查询
     * @param searchMap
     * @param page
     * @param size
     * @return
     */
    @Override
    public Page<Category> findPage(Map<String, Object> searchMap, int page, int size) {
        PageHelper.startPage(page, size);
        Example example = createExample(searchMap);
        return (Page<Category>) categoryMapper.selectByExample(example);
    }

    @Override
    public List<Category> findCategoryByModule(Integer moduleId) {
        return categoryMapper.findCategoryByModule(moduleId);
    }

    /**
     * 构建查询对象
     *
     * @param searchMap
     * @return
     */
    private Example createExample(Map<String, Object> searchMap) {
        Example example = new Example(Category.class);
        Example.Criteria criteria = example.createCriteria();
        if (searchMap != null) {
            // 分类名称
            if (searchMap.get("name") != null && !"".equals(searchMap.get("name"))) {
                criteria.andLike("name", "%" + searchMap.get("name") + "%");
            }
            // 是否显示
            if (searchMap.get("isShow") != null && !"".equals(searchMap.get("isShow"))) {
                criteria.andEqualTo("isShow", searchMap.get("isShow"));
            }
            // 是否导航
            if (searchMap.get("isMenu") != null && !"".equals(searchMap.get("isMenu"))) {
                criteria.andLike("isMenu", "%" + searchMap.get("isMenu") + "%");
            }

            // 分类ID
            if (searchMap.get("id") != null) {
                criteria.andEqualTo("id", searchMap.get("id"));
            }
            // 视频数量
            if (searchMap.get("videoNum") != null) {
                criteria.andEqualTo("videoNum", searchMap.get("videoNum"));
            }
            // 排序
            if (searchMap.get("seq") != null) {
                criteria.andEqualTo("seq", searchMap.get("seq"));
            }
            // 模板ID
            if (searchMap.get("moduleId") != null) {
                criteria.andEqualTo("moduleId", searchMap.get("moduleId"));
            }

        }
        return example;
    }

}
